package org.example.netty.buf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;

/**
 * 常用api
 */
public class Demo2 {
    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.copiedBuffer("hello,world!".getBytes());
        //判空
        if (byteBuf.hasArray()) {
            //获取全部
            byte[] array = byteBuf.array();
            System.out.println(new String(array));
            //真实的类型
            System.out.println(byteBuf);
            //数组偏移量
            System.out.println(byteBuf.arrayOffset());
            //写入索引
            System.out.println(byteBuf.writerIndex());
            //读取索引
            System.out.println(byteBuf.readerIndex());
            //长度
            System.out.println(byteBuf.capacity());
            //可读长度
            System.out.println(byteBuf.readableBytes());
            //获取(非读取)中间,4开启长度6
            System.out.println(byteBuf.getCharSequence(4, 6, StandardCharsets.UTF_8));
        }
    }
}
